package cn.sciento.export.util;

import java.util.ArrayList;
import java.util.List;

import cn.sciento.core.util.Pair;

public class ShardUtils {
    public static Pair<Integer, Integer> shard(int size, int shardSize) {
        int shardNum, lastNum = size % shardSize;
        if (lastNum == 0) {
            shardNum = size / shardSize;
            lastNum = shardSize;
        } else {
            shardNum = size / shardSize + 1;
        }
        return new Pair(Integer.valueOf(shardNum), Integer.valueOf(lastNum));
    }

    public static List<List<?>> prepareShardData(List<?> data, int shardNum, int shardSize, int lastNum) {
        List<List<?>> shardData = new ArrayList<>();
        List<?> shard = null;
        for (int i = 0; i < shardNum; i++) {
            if (i == shardNum - 1) {
                shard = data.subList(i * shardSize, i * shardSize + lastNum);
            } else {
                shard = data.subList(i * shardSize, (i + 1) * shardSize);
            }
            shardData.add(shard);
        }
        return shardData;
    }
}
